package com.jokins.admin.ios.articles.action;

import com.alibaba.fastjson2.JSON;
import com.jokins.admin.ios.articles.actioncontext.GetBookArticlesListDoc;
import com.jokins.admin.ios.articles.mapper.BookArticlesMapper;
import com.jokins.admin.ios.articles.param.BookArticlesParam;
import com.jokins.admin.ios.articles.vo.BookArticlesListVo;
import com.jokins.common.actions.action.ActionContext;
import com.jokins.common.actions.action.BaseAction;
import com.jokins.common.actions.exception.BusinessException;
import com.jokins.common.constant.AccountEnum;
import com.jokins.common.util.SqlTypeConverter;
import com.jokins.common.util.date.DateFormatter;
import com.jokins.common.util.date.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 〈〉<br>
 *
 * @author Mr.Wang
 * @create 2025/2/6
 */
@Slf4j
@Component
public class GetBookArticlesListDocAction extends BaseAction<GetBookArticlesListDoc> {

    @Autowired
    private BookArticlesMapper articlesMapper;

    @Override
    protected void process(GetBookArticlesListDoc actionContext) throws BusinessException {
        log.info("进入action。。。。。");
        BookArticlesParam param=actionContext.getParam();
        log.info("Executing GetBookArticlesList SQL query...");
        // Date[] range = DateUtils.getSevenDaysDateRange();
        // SimpleDateFormat sd=new SimpleDateFormat(DateFormatter.DATE_FORMAT_YYYY_MM_DD_HH_MM_SS);
        // param.setBeginTime(sd.format(range[0]));
        // param.setEndTime(sd.format(range[1]));
        List<BookArticlesListVo> bookArticleList = articlesMapper.selectBookArticlesLis(param);
        List<BookArticlesListVo> bookArticlesListVoList=new ArrayList<>();
        for (BookArticlesListVo bookArticle : bookArticleList) {
            BookArticlesListVo vo=new BookArticlesListVo();
            SqlTypeConverter.convertEntity(bookArticle,vo, DateFormatter.DATE_FORMAT_YYYY_MM_DD_HH_MM_SS);
            String accountType = AccountEnum.getChineseNameByEnglish(bookArticle.getAccountType()).orElse("未知会员类型");
            vo.setAccountType(accountType);
            bookArticlesListVoList.add(vo);
        }
        log.info("sysUserVoList:{}", JSON.toJSONString(bookArticlesListVoList));
        actionContext.setResult(ActionContext.RESULT_DOC_KEY,bookArticlesListVoList);
    }
}